package models

import (
	"gin-ebook/Databases"
	"gorm.io/gorm"
	"log"
	"time"
)

type Dynamic struct {
	Databases.Model

	Title     string `json:"title"`
	Desc      string `json:"desc"`
	CreatedBy string `json:"created_by"`
}

func AddDynamic(data map[string]interface{}) {
	dynamic := Dynamic{
		Title:     data["title"].(string),
		Desc:      data["desc"].(string),
		CreatedBy: data["created_by"].(string),
	}
	db.Create(&dynamic)
}

func GetDynamic(pageNum int, pageSize int, maps interface{}) (dynamic []Dynamic) {
	db.Where(maps).Limit(pageSize).Offset(pageNum).Find(&dynamic)
	return
}

func GetDynamicTotal(maps interface{}) (count int64) {
	db.Model(&Dynamic{}).Where(maps).Count(&count)
	return
}
func (dynamic *Dynamic) BeforeCreate(tx *gorm.DB) (err error) {

	dynamic.CreatedOn = time.Now().Unix()
	log.Println(dynamic.CreatedOn)

	return
}

// hook
func (dynamic *Dynamic) BeforeUpdate(tx *gorm.DB) (err error) {

	dynamic.CreatedOn = time.Now().Unix()
	log.Println(dynamic.CreatedOn)

	return
}
